<template>
  <!-- 1 ytq用户列表 -->
  <div class="search">
    <div class="breadcrumb">
      <Breadcrumb :aaa="aaa" :msgg="msg" :txt="txt" :path1="path1" />
    </div>

    <div class="con_container">
      <el-card>
        <div class="search_top">
          <span> 筛选搜索</span>
          <span>
            <el-button @click="onClear">重置</el-button>
            <el-button type="primary" @click="onSearch">查询搜索</el-button>
          </span>
        </div>
        <el-form label-width="100px">
          <el-form-item label="账号/姓名">
            <el-input
              class="ipt"
              type="text"
              placeholder="请输入用户名"
              v-model="name"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-card>

      <el-card class="btn">
        <div
          class="btn_in"
          style="display: flex; justify-content: space-between"
        >
          <div style="margin-top: 10px">数据列表</div>
          <div>
            <el-button @click="onAddUser" type="primary">添加</el-button>
          </div>
        </div>
      </el-card>

      <!-- 数据的展示 -->
      <el-table :data="tableData" style="width: 100%" :border="true">
        <el-table-column prop="id" label="编号" width="100" />
        <el-table-column prop="username" label="账号" />
        <el-table-column prop="nickName" label="姓名" />
        <el-table-column prop="email" label="邮箱" />
        <!-- 自定义列模板 -->
        <el-table-column label="添加时间">
          <!-- slot 插槽写法  scope就是作用域插槽 ， scope.row 就是数据 -->
          <template #default="scope">
            <div>
              {{
                scope.row.createTime
                  ? dayjs(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss")
                  : "/"
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="最后登录">
          <!-- slot 插槽写法  scope就是作用域插槽 ， scope.row 就是数据 -->
          <template #default="scope">
            <div>
              {{
                scope.row.loginTime
                  ? dayjs(scope.row.loginTime).format("YYYY-MM-DD HH:mm:ss")
                  : ""
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否启用">
          <template #default="scope">
            <el-switch :value="scope.row.status === 1 ? true : false" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" flex="right">
          <template #default="scope">
            <el-button>分配角色</el-button>
            <el-button @click="onEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <el-pagination
        class="pagination"
        v-model:current-page="pagination.pageNum"
        v-model:page-size="pagination.pageSize"
        background
        :page-sizes="[1, 5, 10, 20]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

      <!-- 编辑组件 -->
      <Edit
        :visible="visible"
        :form="modalForm"
        @cancel="onCloseDialog"
        @confirm="onConfirmDialog"
      ></Edit>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import type { Ref } from "vue";
import * as api from "../../request/api";
import useTable from "../../hooks/useTables";
import dayjs from "dayjs";
import Edit from "../../components/Edit.vue";
import type { IAdminListItem } from "../../request/QmsAdminConreiller/index";
import { ElMessage, ElMessageBox } from "element-plus";
import Breadcrumb from "../../components/breadcrumb.vue";
let msg = ref("用户列表");
let txt = ref("权限");
let path1 = ref("ums/admin");
let aaa = ref(0);

let name = ref("");
let add = false; // false编辑 true 新增
let modalForm = ref({}) as Ref<IAdminListItem>;

let {
  tableData,
  getTableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
} = useTable(api.admin.getAdminList, {
  // keyword: name.value  // 可以删掉 name.value 不是一个响应式数据
  keyword: name, // name 是响应式的
});

// 弹框
let visible = ref(false);

// 添加用户
let onAddUser = () => {
  let newUser = {
    email: "",
    nickName: "",
    note: "",
    password: "",
    status: 0,
    username: "",
  } as IAdminListItem;
  modalForm.value = newUser;
  add = true;
  visible.value = true;
};
// 编辑
let onEdit = (row: IAdminListItem) => {
  // console.log(row);
  add = false;
  modalForm.value = row;
  // console.log(modalForm);
  visible.value = true;
};
//重置
let onClear = () => {
  name.value = "";
};
// 子级emit组件传过来的事件
let onCloseDialog = () => {
  visible.value = false;
};

// 确认了输入框的内容之后
// 需要发送请求
let onConfirmDialog = (row: IAdminListItem) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否确认提交!", "提示", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async () => {
      // 发请求
      // 去判断 是 编辑 还是添加
      if (add) {
        await api.admin.adminRegister(row); // 新增
      } else {
        await api.admin.adminUpdate(row); // 编辑
      }

      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};

getTableData();
let onSearch = async () => {
  console.log(name.value);
  getTableData();
  // 发起请求 ，搜索数据
  // let listData = await api.admin.getAdminList({ keyword: name.value })
};
</script>

<style scoped lang="scss">
.search {
  background-color: #fff;
  margin-bottom: 20px;

  .search_top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}

.btn {
  background-color: #fff;
  margin-bottom: 20px;
  box-sizing: border-box;
  margin-top: 20px;
  width: 100% !important;
}

/* :deep .el-input {
  width: 200px !important;
} */

:deep .el-button {
  font-size: 12px !important;
}

.breadcrumb {
  height: 45px;
  border-bottom: 1px solid #ebeef5;
  font-size: 20px;
}

.con_container,
el-table {
  padding: 10px 20px !important;
  margin-top: 25px !important;
}

:deep .el-table .el-table__cell {
  text-align: center !important;
}

:deep .el-table thead {
  font-weight: bold !important;
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}

.ipt {
  width: 200px;
}
</style>
